package com.woniu.adopt.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniu.adopt.entity.Apply;
import com.woniu.adopt.entity.PetAdopt;
import com.woniu.adopt.mapper.ApplyMapper;
import com.woniu.adopt.mapper.ManagerMapper;
import com.woniu.adopt.mapper.MessageMapper;
import com.woniu.adopt.vo.EchartsVo;
import com.woniu.adopt.vo.ApplyVo;
import com.woniu.adopt.vo.ManagerVo;
import com.woniu.adopt.vo.MessageVo;
import com.woniu.adopt.webSocket.WebSocketServer;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 宠物领养申请service
 */
@Service
public class ApplyService {
    @Autowired
    ApplyMapper applyMapper;
    @Autowired
    ManagerMapper managerMapper;
    @Autowired
    MessageMapper messageMapper;
    @Autowired
    WebSocketServer webSocketServer;

    /**
     * 分页查询宠物领养申请
     *
     * @param pageNum  当前页
     * @param pageSize 每页数量
     * @param name     用户名称
     * @param time     申请时间
     */
    public PageInfo<ApplyVo> queryPetAdoptByPage(Integer pageNum, Integer pageSize, String name, Date time) {
        PageHelper.startPage(pageNum, pageSize).setOrderBy("a.insert_time");
        List<ApplyVo> applys = applyMapper.selectApplyByCondition(name, time);
        return new PageInfo<>(applys);
    }

    /**
     * 修改status
     *
     * @param id
     * @param status
     * @param date
     */
    @Transactional
    public void modify(Integer id, Integer status, Date date) {
        String str = null;//回复前台的消息
        MessageVo message = new MessageVo();
        message.setInsertTime(date);

        //通过审核，先用用户端账号注册到后台，然后将该账号授予角色“志愿者”和‘普通用户’
        //1.得到前台用户账号、昵称和密码
        ApplyVo applyVo = applyMapper.selectByPrimaryKey(id);
        if (status == 1) {
            String userName = applyVo.getUserName();
            String username = applyVo.getUserAccountName();
            String password = applyVo.getUserPassword();
            //2.注册到manager表中，赋予角色志愿者
            ManagerVo managerVo = new ManagerVo();
            managerVo.setUsername(username);
            managerVo.setPassword(password);
            managerVo.setName(userName);
            managerVo.setInsertTime(new Date());
            managerVo.setUpdateTime(new Date());
            managerMapper.insertManager(managerVo);
            //授予志愿者角色和普通用户
            managerMapper.insertRoleToManager(managerVo.getId(), new int[]{3, 4});

            //回复消息
            str = "亲爱的用户: 您的志愿者申请已通过，新的管理员账号与密码与本用户相同。欢迎加入本团队!";

        }
        if (status == 2) {
            //回复消息
            str = "亲爱的用户: 很遗憾的通知您，您的志愿者申请未通过!";
        }
        //
        applyMapper.updateByStatus(id, status, date);
        //存入数据库
        message.setUserId(applyVo.getUserId());
        message.setMessage(str);
        messageMapper.insertMessage(message);
        //发送消息给前台
        webSocketServer.sendOneMessage(String.valueOf(applyVo.getUserId()), str);

    }

    /**
     * 分页查询历史宠物领养申请
     *
     * @param pageNum  当前页
     * @param pageSize 每页数量
     * @param name     用户名称
     * @param time     申请时间
     * @param status
     */
    public PageInfo<ApplyVo> queryOldPetAdoptByPage(Integer pageNum, Integer pageSize, String name, Date time, Integer status) {
        PageHelper.startPage(pageNum, pageSize).setOrderBy("a.update_time desc");
        List<ApplyVo> applys = applyMapper.selectOldApplyByCondition(name, time, status);
        return new PageInfo<>(applys);
    }

    public Integer queryApplyByNum() {
        return applyMapper.count();
    }

    public List<EchartsVo> queryWeekNum() {
        return applyMapper.weekNumByCount();
    }
}
